﻿using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;

namespace NRemedy
{
    public class GenerateProperty
    {
        public void Create(CodeTypeDeclaration classType,string propertyName,Type propertyType , Dictionary<string,object> attributePropertyValues)
        {
            if (classType == null) throw new ArgumentNullException("classType");
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
            if (propertyType == null) throw new ArgumentNullException("propertyType");

            CodeMemberProperty _property = new CodeMemberProperty();
            _property.Name = propertyName;
            _property.Type = new CodeTypeReference(propertyType);
            _property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            string fieldName = "_" + propertyName;
            _property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)));
            _property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName), new CodePropertySetValueReferenceExpression()));

            //add attribute
            GeneratePropertyAttribute gpa = new GeneratePropertyAttribute();
            gpa.Create(_property, typeof(ARFieldMappingAttribute), attributePropertyValues);

            classType.Members.Add(_property);
        }
    }
}
